<template>
  <div class="app-container">
    <div class="content-container">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="关键字">
            <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <div class="mb-10px">
            <el-button type="success" icon="plus" @click="handleOpenDialog()">新增</el-button>
          </div>

          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handleQuery="changePage"
          >
            <template #status="{ row, name }">
              <el-switch
                v-model="row.status"
                :active-value="1"
                :inactive-value="0"
                :disabled="row.userType === '24'"
                :title="getLabelByValue(UserStatusEnum, row.status)"
                @change="
                  ($event) => {
                    handleChange($event, row);
                  }
                "
              ></el-switch>
            </template>
            <template #gender="{ row, name }">
              {{ getLabelByValue(GenderEnum, row.userProfileVO.gender) }}
            </template>
            <template #avatar="{ row, name }">
              <el-avatar :size="50" :src="row.userProfileVO.avatar" />
            </template>
            <el-tag
              :type="UserStatusEnum.ENABLED.value === row.userProfileVO.status ? 'success' : 'info'"
            >
              {{ getLabelByValue(UserStatusEnum, row.userProfileVO.status) }}
            </el-tag>
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleEdit(row)">编辑</el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(row)">
                删除
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <partner-user-form
      v-model="formVsisble"
      :data="currentRow"
      @load-data="handleQuery"
    ></partner-user-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import UserAPI from "@/api/system/user";
import UserForm from "@/views/system/employee/components/user-form.vue";
import { Message } from "@element-plus/icons-vue";
import PartnerUserAPI from "@/api/system/partner-user";
import PartnerUserForm from "@/views/system/partner/components/partner-user-form.vue";
import { getLabelByValue } from "@/utils/enums";
import { GenderEnum, UserStatusEnum } from "@/enums/system/UserEnum";

const loading = ref(false);
const formVsisble = ref(false);
const total = ref(0);
const queryFormRef = ref();
const currentRow = ref();

const queryParams = reactive({
  keywords: "",
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref([]);
const tableColumnsData = ref([
  {
    prop: "userProfileVO.username",
    label: "用户名",
    showOverflowTooltip: true,
  },
  {
    prop: "userProfileVO.nickname",
    label: "用户昵称",
    showOverflowTooltip: true,
  },
  {
    prop: "userProfileVO.mobile",
    label: "手机号",
    width: 200,
    showOverflowTooltip: true,
  },
  {
    prop: "userProfileVO.avatar",
    label: "头像",
    width: 200,
    showOverflowTooltip: true,
    customSlot: "avatar",
  },
  /*{
    prop: "userProfileVO.status",
    label: "用户状态",
    width: 200,
    showOverflowTooltip: true,
  },*/
  {
    prop: "userProfileVO.roleNames",
    label: "角色名称",
    width: 200,
    showOverflowTooltip: true,
  },
  {
    prop: "status",
    label: "用户状态",
    width: 200,
    showOverflowTooltip: true,
    customSlot: "status",
  },
  {
    prop: "remark",
    label: "备注",
    width: 200,
    showOverflowTooltip: true,
  },
  {
    prop: "userProfileVO.createTime",
    label: "创建时间",
    width: 150,
    showOverflowTooltip: true,
  },
]);
// 查询
async function handleQuery() {
  try {
    loading.value = true;
    const data = await PartnerUserAPI.getPage(queryParams);
    tableData.value = data.list || [];
    total.value = data.total;
  } finally {
    loading.value = false;
  }
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}
function handleOpenDialog() {
  formVsisble.value = true;
}
function handleEdit(val) {
  currentRow.value = val;
  formVsisble.value = true;
}
async function handleChange($event, row) {
  try {
    const params = {
      status: $event,
      id: row.id,
    };
    await PartnerUserAPI.updateStatus(params);
    handleQuery();
  } catch (e) {
    console.log(e);
  }
}
async function handleDelete(val) {
  const res = await ElMessageBox.confirm("确认删除该项配置?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });

  if (!res) return;
  loading.value = true;
  try {
    await UserAPI.delete(val.id);
    handleQuery();
    Message.success("删除成功");
  } catch (e) {
    Message.success("删除失败");
  } finally {
    loading.value = false;
  }
}

function changePage(val: any) {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
}

onMounted(async () => {
  handleQuery();
});
</script>
